package com.xjy.web.controller.controller;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;

import lombok.SneakyThrows;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.xjy.common.annotation.RepeatSubmit;
import com.xjy.common.annotation.Log;
import com.xjy.common.core.controller.BaseController;
import com.xjy.common.core.domain.AjaxResult;
import com.xjy.common.enums.BusinessType;
import com.xjy.system.domain.vo.SysDatabaseBackupVo;
import com.xjy.system.domain.bo.SysDatabaseBackupQueryBo;
import com.xjy.system.domain.bo.SysDatabaseBackupAddBo;
import com.xjy.system.domain.bo.SysDatabaseBackupEditBo;
import com.xjy.system.service.ISysDatabaseBackupService;
import com.xjy.common.utils.poi.ExcelUtil;
import com.xjy.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 系统-备份Controller
 *
 * @author xjy
 * @date 2022-03-28
 */
@Api(value = "系统-备份控制器", tags = {"系统-备份管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/backup")
public class SysDatabaseBackupController extends BaseController {

    private final ISysDatabaseBackupService iSysDatabaseBackupService;

    /**
     * 查询系统-备份列表
     */
    @ApiOperation("查询系统-备份列表")
    @PreAuthorize("@ss.hasPermi('system:backup:list')")
    @GetMapping("/list")
    public TableDataInfo<SysDatabaseBackupVo> list(@Validated SysDatabaseBackupQueryBo bo) {
        return iSysDatabaseBackupService.queryPageList(bo);
    }

    /**
     * 导出系统-备份列表
     */
    @ApiOperation("导出系统-备份列表")
    @PreAuthorize("@ss.hasPermi('system:backup:export')")
    @Log(title = "系统-备份", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<SysDatabaseBackupVo> export(@Validated SysDatabaseBackupQueryBo bo) {
        List<SysDatabaseBackupVo> list = iSysDatabaseBackupService.queryList(bo);
        ExcelUtil<SysDatabaseBackupVo> util = new ExcelUtil<SysDatabaseBackupVo>(SysDatabaseBackupVo.class);
        return util.exportExcel(list, "系统-备份");
    }

    /**
     * 获取系统-备份详细信息
     */
    @ApiOperation("获取系统-备份详细信息")
    @PreAuthorize("@ss.hasPermi('system:backup:query')")
    @GetMapping("/{id}")
    public AjaxResult<SysDatabaseBackupVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iSysDatabaseBackupService.queryById(id));
    }

    /**
     * 新增系统-备份
     */
    @ApiOperation("新增系统-备份")
    @PreAuthorize("@ss.hasPermi('system:backup:add')")
    @Log(title = "系统-备份", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add() {
        return toAjax(iSysDatabaseBackupService.insertByAddBo() ? 1 : 0);
    }

    /**
     * 修改系统-备份
     */
    @ApiOperation("修改系统-备份")
    @PreAuthorize("@ss.hasPermi('system:backup:edit')")
    @Log(title = "系统-备份", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody SysDatabaseBackupEditBo bo) {
		Thread thread = new Thread(new Runnable() {
			@SneakyThrows
			@Override
			public void run() {
				iSysDatabaseBackupService.updateByEditBo(bo);
			}
		});
		thread.start();
        return toAjax(true);
    }

    /**
     * 删除系统-备份
     */
    @ApiOperation("删除系统-备份")
    @PreAuthorize("@ss.hasPermi('system:backup:remove')")
    @Log(title = "系统-备份" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iSysDatabaseBackupService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
